package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.context.LoginUser;
import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.model.entity.GraphInfo;
import com.atguigu.lease.model.entity.LabelInfo;
import com.atguigu.lease.model.entity.ViewAppointment;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.app.mapper.GraphInfoMapper;
import com.atguigu.lease.web.app.mapper.LabelInfoMapper;
import com.atguigu.lease.web.app.mapper.ViewAppointmentMapper;
import com.atguigu.lease.web.app.service.GraphInfoService;
import com.atguigu.lease.web.app.service.ViewAppointmentService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.appointment.AppointmentDetailVo;
import com.atguigu.lease.web.app.vo.appointment.AppointmentItemVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.text.Utilities;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【view_appointment(预约看房信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ViewAppointmentServiceImpl extends ServiceImpl<ViewAppointmentMapper, ViewAppointment>
        implements ViewAppointmentService {

    @Autowired
    ViewAppointmentMapper viewAppointmentMapper;

    @Autowired
    LabelInfoMapper labelInfoMapper;

    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    GraphInfoMapper graphInfoMapper;
    @Override
    public List<AppointmentItemVo> listAppointMent() {
        LoginUser loginUser = LoginUserContext.getLoginUser();
        Long userId = loginUser.getUserId();
        return viewAppointmentMapper.listAppointMent(userId);
    }

    @Override
    public AppointmentDetailVo getAppointmentDeta(Long id) {
        //ApartmentInfo  被目标继承的类先查出来
        ViewAppointment viewAppointment = viewAppointmentMapper.selectById(id);
        AppointmentDetailVo appointmentDetailVo = new AppointmentDetailVo();
        BeanUtils.copyProperties(viewAppointment,appointmentDetailVo);

        //根据公寓ID查询 公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(viewAppointment.getApartmentId());

        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentItemVo);

        //查LabelInfo  list
        List<LabelInfo> labelInfoList = labelInfoMapper.listByapid(viewAppointment.getApartmentId());
        apartmentItemVo.setLabelInfoList(labelInfoList);

        //查GraphInfo  list
        LambdaQueryWrapper<GraphInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        lambdaQueryWrapper.eq(GraphInfo::getItemId,viewAppointment.getApartmentId());
        List<GraphInfo> graphInfos = graphInfoMapper.selectList(lambdaQueryWrapper);
        apartmentItemVo.setGraphVoList(graphInfos);

        //minRent
        BigDecimal minRent = apartmentInfoMapper.getMinRent(viewAppointment.getApartmentId());
        apartmentItemVo.setMinRent(minRent);

        appointmentDetailVo.setApartmentItemVo(apartmentItemVo);
        return appointmentDetailVo;
    }
}




